﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TV_Organizer.Properties;

namespace TV_Organizer.FileScanner
{
    public static class FolderNameParser
    {
        private static IEnumerable<Part> _parts = new Part[]
        {
            new Part("%show",     "The name of the TV show",               (f) => f.SelectedShow != null ? f.SelectedShow.Name : string.Empty),
            new Part("%season2",  "The season of the TV show (2 digits)",  (f) => f.Season.ToString("D2")),
            new Part("%season",   "The season of the TV show",             (f) => f.Season.ToString()),
            new Part("%episode2", "The episode of the TV show (2 digits)", (f) => f.Episode.ToString("D2")),
            new Part("%episode",  "The episode of the TV show",            (f) => f.Episode.ToString()),
        };
        public static IEnumerable<Part> Parts { get { return _parts; } }

        /// <summary>
        /// Get the folder name in which the file should be moved.
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public static string GetFolderName(File file)
        {
            var path = Settings.Default.TVShowFolderPath;

            foreach (var part in Parts)
            {
                path = part.ParseFolder(path, file);
            }

            return path.RemoveChars(System.IO.Path.GetInvalidPathChars());
        }

        public class Part
        {
            private Func<File, string> _function;

            public Part(string replacedString, string description, Func<File, string> function)
            {
                this.ReplacedString = replacedString;
                this.Description = description;
                _function = function;
            }

            public string ReplacedString { get; private set; }
            public string Description { get; private set; }

            public string ParseFolder(string path, File file)
            {
                return path.Replace(this.ReplacedString, _function(file));
            }
        }
    }
}
